<?php
$units_obj = new Unit();
$prices_obj = new Price();
$images_obj = new Image();
$categories_obj = new Category();

function show_units(){
	global $units_obj, $KToolBar;
	
	$query = "SELECT * FROM `".$units_obj->get_table_name()."` ORDER BY `id` DESC";
	$paginate = paginate($units_obj, $query, $_GET['pg_no'], $limit);
	$units = $paginate['obj'];
//	$units = $units_obj->find("all");
	
	include_once 'views/units.html.php';
}

function filter_units(){
	global $units_obj, $KToolBar;
	$limit = $_POST['limit'];
	$sql = "SELECT * FROM `".$units_obj->get_table_name()."`";
//	$units = $units_obj->select_row_query("SELECT * FROM `".$units_obj->get_table_name()."` LIMIT 0, $limit", "");
	$paginate = paginate($units_obj, $sql, $_GET['pg_no'], $limit);
	$units = $paginate['obj'];
	
	include_once 'views/units.html.php';
}

function new_unit(){
	global $KToolBar;
	$customers_obj = new Customer();
	$managers = $customers_obj->find(array("user_type"=>"manager"));
	
	include_once 'views/units_new.html.php';
}

function save_new_unit(){
	global $units_obj, $notice;
	if($_POST['unit']['is_hotel']) $_POST['unit']['is_hotel'] = 1;
	if($_POST['unit']['is_group']) $_POST['unit']['is_group'] = 1;
	
	if($_POST['unit']['address'] == ""){
		$notice['error'] = 1;
		$notice['msg'] = "The Address field is required";
	}
	else if($_POST['unit']['title'] == ""){
		$notice['error'] = 1;
		$notice['msg'] = "The Title field is required";
	}
	else {
		$save_unit_msg = $units_obj->insert_or_update($_POST['unit'],"");
		$notice['error'] = 0;
		$notice['msg'] = "The unit has been created. Now you can <a href=\"?option=".COMPONENT_NAME."&page=units&action=edit&uid=".mysql_insert_id()."\">edit</a> the details of this unit.";
	}
}

function show_edit_units(){
	global $units_obj, $categories_obj, $images_obj, $prices_obj, $KToolBar;
	$uid = $_GET['uid'];
	$unit = $units_obj->find((int)$uid);
	$unit->amenities = explode(",", $unit->amenities);
	
	$images = $images_obj->select_row_query("SELECT * FROM `".$images_obj->get_table_name()."` WHERE `uid` = '$uid' ORDER BY `order` ASC", "");
	
	$categories = $categories_obj->find("all");
	
	$customers = new Customer();
	$managers = $customers->find(array("user_type" => "manager"));
	
	$prices = $prices_obj->select_row_query("SELECT * FROM `".$prices_obj->get_table_name()."` WHERE `uid` = '$uid' ORDER BY `id` ASC", "");
	$prices_count = $prices_obj->count("SELECT * FROM `".$prices_obj->get_table_name()."` WHERE `uid` = '".$uid."'");
	
	$amenities_obj = new Amenity();
	$amenities = $amenities_obj->find("all");
	
	include_once 'views/units_edit.html.php';
}

function save_edit_units(){
	global $units_obj, $prices_obj, $notice;
	$uid = $_GET['uid'];
	if(isset($_POST['amenities'])) $_POST['units']['amenities'] = implode(",", $_POST['amenities']);
	
	if($_POST['units']['address'] == "" OR $_POST['units']['title'] == ""){
		$notice['error'] = 1;
		$notice['msg'] = "The fields marked * are necessary";
	}
	else{
		mysql_query("BEGIN");
		$edit_units_msg = $units_obj->insert_or_update($_POST['units'],array('id'=>$uid));
		
		$_POST['prices']['startdate'] = array_filter($_POST['prices']['startdate']);
		$_POST['prices']['enddate'] = array_filter($_POST['prices']['enddate']);
		$_POST['prices']['price'] = array_filter($_POST['prices']['price']);
		$to_update = count($_POST['prices']['id']);
		for($i = 0; $i < $to_update; $i++){
			$price = filter_var($_POST['prices']['price'][$i], FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
			$update_prices_msg = $prices_obj->insert_row_query("UPDATE `".$prices_obj->get_table_name()."` SET `startdate` = '".get_date_from_str($_POST['prices']['startdate'][$i])."', `enddate` = '".get_date_from_str($_POST['prices']['enddate'][$i])."', `price` = '".$_POST['prices']['price'][$i]."' WHERE `id` = '".$_POST['prices']['id'][$i]."'");
			if($update_prices_msg['error'] == 1) break;
		}
		$to_insert = count($_POST['prices']['startdate']);
		for($i = $to_update; $i < $to_insert; $i++){
			$insert_prices_msg = $prices_obj->insert_row_query("INSERT INTO `".$prices_obj->get_table_name()."` SET `uid` = '".$uid."', `startdate` = '".get_date_from_str($_POST['prices']['startdate'][$i])."', `enddate` = '".get_date_from_str($_POST['prices']['enddate'][$i])."', `price` = '".$_POST['prices']['price'][$i]."'");
			if($insert_prices_msg['error'] == 1) break;
		}
		if($edit_units_msg['error'] == 1 || $update_prices_msg['error'] == 1 || $insert_prices_msg['error'] == 1){
			mysql_query("ROLLBACK");
			echo $edit_units_msg['error']."-1st<br/>".$update_prices_msg['error']."-2nd<br/>".$insert_prices_msg['error']."-3rd<br/>";
			$notice['error'] = 1;
			$notice['msg'] = $edit_units_msg['msg']."<br/>".$update_prices_msg['msg']."<br/>".$insert_prices_msg['msg'];
		}
		else{
			mysql_query("COMMIT");
			$notice['error'] = 0;
			$notice['msg'] = "Unit information updated successfully";
		}
	}
}

function image_uploader(){
	global $max_images_per_unit, $images_obj;
	$uid = $_GET['uid'];
	$input = "upload_image";
	$target_path = COMPONENT_PATH."uploads/";
	$uploaded_image = upload_image($input, $target_path);
	
	var_dump($uploaded_image);
	
	if($uploaded_image['error'] != 1){
		$images_count = $images_obj->count("SELECT * FROM `".$images_obj->get_table_name()."` WHERE `uid` = $uid");
		if($images_count < $max_images_per_unit){
			
				$unit_images_msg = $images_obj->insert_row_query("INSERT INTO `".$images_obj->get_table_name()."` VALUES(null, '$uid', '".$uploaded_image['path']."', '".$uploaded_image['thumbnails_path']."', '".$uploaded_image['originals_path']."', '', 1)");
				$inserted_id = mysql_insert_id();
				$unit_images_msg = $images_obj->insert_row_query("UPDATE `".$images_obj->get_table_name()."` SET `order` = '".$inserted_id."' WHERE `id` = '".$inserted_id."'");
				$uploaded_image['id'] = $inserted_id;
				$uploaded_image['order'] = $inserted_id;
		}
		else{
			$uploaded_image['error'] = 1;
			$uploaded_image['msg'] = "You cannot upload more than ". $max_images_per_unit ." images per unit.";
		}
	}
	
	include_once 'views/upload_image.html.php';
}

function delete_units(){
	global $units_obj, $notice;
	$notice = delete_selected_items($_POST['units']['ids'], $units_obj);
	if($notice['error'] == 0) $notice['msg'] = "The selected units have been deleted";
}
?>